package com.tianchi.lexiang.background;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.background.util.HddDataBaseVersion;
import com.tianchi.lexiang.background.util.PublicUtil;
import com.tianchi.lexiang.bean.LeXiangConstants;

public class DatabaseOperaction extends Activity implements OnItemClickListener {
	
	private static final String TAG = "DatabaseOperaction";
	private ListView backgroundSettingList;
	private Button backBtn;
	private ProgressDialog progressDialog;
	
	private PublicUtil publicUtil = PublicUtil.getPulibUtil();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.background_setting);
		initView();
	}
	
	private void initView() {
		backgroundSettingList = (ListView) findViewById(R.id.background_setting_list);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, this.getResources().getStringArray(R.array.database_operaction));
		backgroundSettingList.setAdapter(adapter);
		backgroundSettingList.setOnItemClickListener(this);
		backBtn = (Button) findViewById(R.id.back_btn);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DatabaseOperaction.this.finish();
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		switch (position) {
		case 0:
			dialog(getString(R.string.is_not_import_database));
			break;
		case 1 :
			dialog(getString(R.string.is_not_import_file));
		default:
			break;
		}
	}
	
	private void dialog(final String message){
		AlertDialog.Builder builder = new Builder(DatabaseOperaction.this);
		builder.setMessage(message);
		builder.setIcon(android.R.drawable.ic_menu_info_details);
		builder.setTitle(getString(R.string.tip));
		builder.setPositiveButton(getString(R.string.yes), new Dialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				if(getString(R.string.is_not_import_database).equals(message)){
					progressDialog = ProgressDialog.show(DatabaseOperaction.this, getString(R.string.udisk_import_database),
							getString(R.string.are_import_database), true);
				}else{
					progressDialog = ProgressDialog.show(DatabaseOperaction.this, getString(R.string.udisk_import_database),
							getString(R.string.are_import_file), true);
				}
				new ImportAndDerviedTask().execute(message);
			}
		});
		builder.setNegativeButton(getString(R.string.no), new Dialog.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	
	class ImportAndDerviedTask extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			if(getString(R.string.is_not_import_database).equals(params[0])){
				File mFile =new File(LeXiangConstants.DATABASE_IN_UDISK);
				if(mFile.exists()){
					if(publicUtil.copyInfo(LeXiangConstants.DATABASE_IN_UDISK, LeXiangConstants.NEW_DATABASE_PATH)){
						File file= new File(LeXiangConstants.NEW_DATABASE_PATH);
						file.renameTo(new File(LeXiangConstants.DERIVED_DATABASE_PATH, LeXiangConstants.DATABASE_NAME));
					}
					return getString(R.string.confirm);
				}else{
					return getString(R.string.check_udisk_is_eixt_database);
				}
			}else{
				File mFile =new File(LeXiangConstants.FILE_IN_UDISK);
				if(mFile.exists()){
					if(publicUtil.copyInfo(LeXiangConstants.FILE_IN_UDISK, LeXiangConstants.FILE_IN_HDD)){
						return getString(R.string.file_copy_success);
					}else{
						return getString(R.string.file_copy_failure);
					}
				}else{
					return getString(R.string.check_udisk_is_eixt_database);
				}
			}
		}

		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if(getString(R.string.confirm).equals(result)){
				progressDialog.dismiss();
				HddDataBaseVersion.setCurrentHddDataBaseVersion();
				promptRebootDialog();
			}else{
				progressDialog.dismiss();
				Toast.makeText(DatabaseOperaction.this, result, Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	  private void promptRebootDialog(){
	    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	builder.setTitle(R.string.reboot_prompt_title);
	    	builder.setMessage(R.string.reboot_prompt_message);
	    	builder.setPositiveButton(R.string.confirm_reboot, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					reboot();
				}
			});
	    	builder.create();
	    	builder.show();
	    }
	    
	    private void reboot(){
	    	Log.d(TAG, "will reboot the device");
	    	Intent i = new Intent(Intent.ACTION_REBOOT);   
	    	i.putExtra("nowait", 1);   
	    	i.putExtra("interval", 1);   
	    	i.putExtra("window", 0);   
	    	sendBroadcast(i);
	    }
	

}
